package www.com.library.dialog;


import android.app.Activity;
import android.app.Dialog;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;


import java.util.Timer;
import java.util.TimerTask;

import www.com.common.library.R;

/**
 * 第一次使用某个界面时弹出的帮助层
 * 
 */
public class FirstUserDialog extends Dialog implements OnTouchListener {
	
	protected RelativeLayout tipsLayout = null;
	private boolean isAutoHidden = false;//是否需要自动隐藏
	private Timer autoHiddenTimer = null;//自动隐藏的计时器
	private Activity rootActivity = null;
	
	/**
	 * @param activity
	 * @param imageID 图片资源
	 */
	public FirstUserDialog(Activity activity, int imageID) {
		//为了让dialog充满全屏，背景透明，使用该样式
		super(activity, R.style.dialog_fullscreen);
		setOwnerActivity(activity);	
		this.rootActivity = activity;
	    this.isAutoHidden = true;
	    initDialog(true,imageID);
	}
	
	/**
	 * @param activity
	 */
	public FirstUserDialog(Activity activity) {
		//为了让dialog充满全屏，背景透明，使用该样式
		super(activity, R.style.dialog_fullscreen);
		setOwnerActivity(activity);	
		this.rootActivity = activity;
	    initDialog(false,0);
	}
	
	/**
	 * @param isCenter 是否全屏标识
	 */
	public void initDialog(boolean isCenter,int mImageID){
		setContentView(R.layout.first_user_tips);
		tipsLayout = (RelativeLayout) findViewById(R.id.tips_layout);
		tipsLayout.setOnTouchListener(this);
		if(isCenter){
			setImageViewCenter(mImageID);
		}else{
			tipsLayout.setBackgroundResource(R.mipmap.common_popup_dark_bg);
		}
	}
	
	/**
	 * 图片全屏居中显示
	 * 
	 * @author yuye.zou
	 * @date 2013-12-20
	 */
	public void setImageViewCenter(int mImageID){
		ImageView tipsImageView = new ImageView(rootActivity);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		tipsImageView.setScaleType(ScaleType.CENTER);
		tipsImageView.setLayoutParams(params);
		tipsImageView.setImageResource(mImageID);
		tipsLayout.addView(tipsImageView);
	}
	
	/**
	 * 根据具体位置设置图片资源
	 * @author yuye.zou
	 * @date 2013-12-20
	 * 
	 * @param marginLeft
	 * @param marginTop
	 * @param marginRight
	 * @param marginButtom
	 * @param mImageID
	 */
	public void setImageViewMarign(int marginLeft,int marginTop,int marginRight,int marginButtom,int mImageID){
		ImageView tipsImageView = new ImageView(rootActivity);
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.setMargins(marginLeft, marginTop, marginRight, marginButtom);
		tipsImageView.setLayoutParams(params);
		tipsImageView.setImageResource(mImageID);
		tipsLayout.addView(tipsImageView);
	}
	
	/**
	 * 设置居下位置后，图片水平居中显示
	 * @date 2017-01-22
	 * @param marginLeft
	 * @param marginTop
	 * @param marginRight
	 * @param marginButtom
	 * @param rule 相对布局中相对的位置，传入参数为RelativeLayout.CENTER_HORIZONTAL等值
	 * @param mImageID 图片资源
	 */
	public void setImageViewMarign(int marginLeft,int marginTop,int marginRight,int marginButtom,int rule,int mImageID){
		ImageView tipsImageView = new ImageView(rootActivity);
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.setMargins(0, 0, 30, 0);
		params.addRule(rule);
		tipsImageView.setLayoutParams(params);
		tipsImageView.setScaleType(ScaleType.FIT_XY);
		tipsImageView.setImageResource(mImageID);
		tipsLayout.addView(tipsImageView);
	}
	
	@Override
	public void show() {
		super.show();
		if(isAutoHidden){
			autoHiddenTimer = new Timer();
			autoHiddenTimer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					dismiss();
				}
			}, 3000);
		}
	}

	@Override
	public void cancel() {
		super.cancel();
		if(null != autoHiddenTimer){
			autoHiddenTimer.cancel();
			autoHiddenTimer = null;
		}
	}	

	@Override
	public void dismiss() {
		super.dismiss();
		if(null != autoHiddenTimer){
			autoHiddenTimer.cancel();
			autoHiddenTimer = null;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		dismiss();
		return super.onTouchEvent(event);
	}	
}
